<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerCode']" placeholder="请输入客户"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerName']" placeholder="请输入客户名称"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番1" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuCode1']" placeholder="请输入品番"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuCode2']" placeholder="请输入品番"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="箱数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['boxNum']" placeholder="请输入箱数"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收容数/箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pcs']" placeholder="请输入箱数"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="件数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pcsNum']" placeholder="请输入箱数"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="标签号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['labelNumber']" placeholder="请输入标签号"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发票号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['invoiceNumber']" placeholder="请输入发票号"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['batchNumber']" placeholder="请输入批次号"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="上架日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择上架日期" v-decorator="['stackingDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="货架" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['shelfCode', validatorRules.shelfCode]" placeholder="请选择货架">
                <a-select-option v-for="item in shelfCodeList" :value="item.shelfCode">
                  {{ item.shelfCode }}
                </a-select-option>
              </a-select>
              <a-input v-decorator="['shelfCode']" placeholder="请输入货架"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="货格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['compartmentCode', validatorRules.compartmentCode]" placeholder="请选择货格">
                <a-select-option v-for="item in compartmentCodeList" :value="item.compartmentCode">
                  {{ item.compartmentCode }}
                </a-select-option>
              </a-select>
              <a-input v-decorator="['compartmentCode']" placeholder="请输入货格"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="order_status" placeholder="请选择状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="出库日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择出库日期" v-decorator="['outDate']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ReportLabelForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/reportLabel/reportLabel/add",
          edit: "/reportLabel/reportLabel/edit",
          queryById: "/reportLabel/reportLabel/queryById",
          getShelf: "/basicShelf/basicShelf/getAll"
        },
        shelfCodeList: [],
        compartmentCodeList: [],
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getShelf();
    },
    methods: {
      getShelf(){
        getAction(this.url.getShelf).then((res)=>{
          if(res.success){
            this.shelfCodeList = res.result;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'customerCode','customerName','skuCode1','skuCode2','boxNum','pcs','pcsNum','labelNumber','inWorkNumber','invoiceNumber','batchNumber','stackingDate','shelfCode','compartmentCode','status','outWorkNumber','outDate'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'customerCode','customerName','skuCode1','skuCode2','boxNum','pcs','pcsNum','labelNumber','inWorkNumber','invoiceNumber','batchNumber','stackingDate','shelfCode','compartmentCode','status','outWorkNumber','outDate'))
      },
    }
  }
</script>